package com.thoughtworks.biblioteca.controller;

import com.thoughtworks.biblioteca.model.MenuItem;
import com.thoughtworks.biblioteca.model.Menu;
import com.thoughtworks.biblioteca.dao.MenuDao;

public class MenuController {

    private MenuDao menuDao;

    public MenuController() {

    }

    public String generateMenuDetailOf() {
        Menu menu = menuDao.loadMenu();
        return getMenuHead() + getMenuBody(menu) + getMenuFoot();
    }

    public void printMenu() {
        System.out.println(generateMenuDetailOf());
    }

    private String getMenuBody(Menu menu) {
        StringBuffer bf = new StringBuffer("");
        for (MenuItem item : menu.getMenuItems()) {
            String menuItemDetail = item.getID() + ". " + item.getMenuName();
            menuItemDetail = formatMenuItem(menuItemDetail);
            bf.append(menuItemDetail).append("\n");
        }

        return bf.toString();
    }

    String formatMenuItem(String tem) {
        String temp = "*         ";
        String template = "                                                        *";
        String tem1 = temp + tem + template;
        tem1 = tem1.substring(0, template.length()) + "*";
        return tem1;
    }

    private String getMenuFoot() {
        return "**********************************************************";

    }

    private String getMenuHead() {
        StringBuffer bf = new StringBuffer("");
        bf.append("**********************************************************\n");
        bf.append("* Welcome to The Bangalore Public Library System - Biblioteca *\n");
        bf.append("**********************************************************\n");
        bf.append("*                Menu                                    *\n");
        bf.append("*         =====================                          *\n");
        return bf.toString();
    }

    public MenuDao getMenuDao() {
        return menuDao;
    }

    public void setMenuDao(MenuDao menuDao) {
        this.menuDao = menuDao;
    }
}
